package com.example.ref;


import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

/**
 * Wraps the using of SQLite database in a simple, easy-to-use way based on java.lang.Reflection.
 * @author Giuseppe.Longordo
 *
 */
public final class RSQLiteDatabase 
{
	private static RSQLiteDatabase mInstance;

	/**
	 * @return The only instance of RSQLiteDatabase object
	 */
	public static RSQLiteDatabase getSingleton()
	{
		if(mInstance==null)
			mInstance= new RSQLiteDatabase();
		return mInstance;
	}

	/**
	 * Creates a simple Select object
	 * @return 'SELECT *'
	 */
	public Select select()
	{
		return new Select("*");
	}

	/**
	 * Creates a Select object and specifies a limit of rows to retrieve
	 * @param limit integer value > 0
	 * @return 'SELECT *' ... 'LIMIT [@limit]'
	 */
	public Select select(int limit)
	{
		Select select=select();
		select.limit=limit;
		return select;
	}
	/**
	 * Creates a Select object with distinct attribute
	 * @return 'SELECT DISTINCT *'
	 */
	public Select selectDistinct()
	{
		return new Select("DISTINCT *");
	}
	/**
	 * Creates a Select object with distinct attribute and specifies a limit of rows to retrieve
	 * @param limit integer value > 0
	 * @return 'SELECT DISTINCT *' ... 'LIMIT [@limit]'
	 */
	public Select selectDistinct(int limit) {
		Select select=selectDistinct();
		select.limit=limit;
		return select;
	}
	
	/**
	 * Creates a Insert object for the passed object @object
	 * @param object the object to insert
	 * @return 'INSERT INTO [@object.class] VALUES [@object]
	 */
	public Insert insert(Object object)
	{
		return new Insert(object);
	}

	/**
	 * Creates a Delete object for a specified class [table]
	 * @param _class 
	 * @return 'DELETE FROM [@class];
	 */
	public <T> Delete<T> deleteFrom(Class<T> _class)
	{
		return new Delete<T>(_class);
	}

	/**
	 * @return An instance of RSQLiteTransaction object that is a List of Statement you can execute all in one time.
	 */
	public RSQLiteTransaction beginTransaction()
	{
		return new RSQLiteTransaction();
	}

	/**
	 * Class for managing a transaction of operation
	 * @author Giuseppe.Longordo
	 *
	 */
	public class RSQLiteTransaction extends ArrayList<Statement>
	{
		private static final long serialVersionUID = 5820981220088157538L;

		/**
		 * Executes the transaction
		 * @param context android context
		 * @return if transaction was committed, returns a list of results, otherwise null
		 */
		public List<Object> executeAll(Context context)
		{
			DatabaseAdapter dbAdapter= new DatabaseAdapter(context);

			SQLiteDatabase db= dbAdapter.open();
			db.beginTransaction();
			try
			{
				int len=size();	 
				List<Object> results= new ArrayList<Object>(len);
				Statement statement;
				for (int j = 0; j < len; j++) {
					statement= get(j);
					results.add(statement.execute(db,context));
				}
				db.setTransactionSuccessful();
				return results;
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
			finally
			{
				if(db!=null)
					db.endTransaction();
				if(dbAdapter!=null)
					dbAdapter.close();
			}
			return null;

		}
	}
}
